﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getsubsubsector" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;

namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class getsubsubsector : SIPO.Web.BaseHandler
    {
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private string MethodName = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {

            base.ProcessRequest(context);

            if (context.Request.Params["method"] != null)
                MethodName = context.Request.Params["method"];

            if (MethodName == "getbyidandsubsectorid")
            {
                int id = Convert.ToInt32(context.Request.Params["subsubsector_id"]);
                int subsectorid = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetList();
                var result = subsubSectors.Where(x => x.SubSubSectorID == id && x.SubSectorID == subsectorid);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else if (MethodName == "getbysubsectorid_AND_subsubsectorids")
            {
                int id = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetListBySubSectorID_AND_SubSubSectorIDs(id, base.SubSubSectorIDs);
                var result = subsubSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result)); ;
            }
            else if (MethodName == "getbyDebtorId")
            {
                int debtorID = Convert.ToInt32(context.Request.Params["debtorid"]);
                List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetListByDebtorID_AND_SubSubSectorIDs(debtorID, base.SubSubSectorIDs);
                var result = subsubSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "getbydebtorid_subsectorid_AND_subsubsectorids")
            {
                int debtorID = Convert.ToInt32(context.Request.Params["debtorid"]);
                int subsectorID = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetListByDebtorID_SubSectorID_AND_SubSubSectorIDs(debtorID, subsectorID, base.SubSubSectorIDs);
                var result = subsubSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "registrasi")
            {
                int id = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetList();
                var result = subsubSectors.Where(x => x.SubSectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "multipleSubSubSectorID")
            {
                string parameter = context.Request.Params["param"];
                string [] Sektor  = parameter.Split('-');
                string Sektors = Sektor.ElementAt(0);
                
                string SektorSubSektors = Sektor.ElementAt(1);
                
                string[] ArrSektor = Sektors.Split(',');
                
                string[] ArrSubSektor = SektorSubSektors.Split(',');
                string parameters = string.Empty;
                bool status = false;
                for (int i = 0; i <ArrSektor.Length; i++)
                {
                    for (int j = 0; j < ArrSubSektor.Length; j++)
                    {
                        if (ArrSubSektor.ElementAt(j).ToString().Trim().Split('.').FirstOrDefault() == ArrSektor.ElementAt(i).Trim())
                        {
                            if(parameters==string.Empty)
                            parameters += " where (recSector.SectorCode = " + ArrSektor.ElementAt(i) + " and recSubSector.SubSectorCode=" + ArrSubSektor.ElementAt(j).ToString().Trim().Substring(2, 1)+")";
                            else
                            parameters += " or (recSector.SectorCode = " + ArrSektor.ElementAt(i) + " and recSubSector.SubSectorCode=" + ArrSubSektor.ElementAt(j).ToString().Trim().Substring(2, 1) + ")";
                            status = true;
                        }
                        
                    }
                    if (status == false)
                    {
                        
                        if (parameters != string.Empty)
                            parameters += " or recSector.SectorCode=" + ArrSektor.ElementAt(i);
                        else
                            parameters += " where recSector.SectorCode=" + ArrSektor.ElementAt(i);

                    }
                    else
                        status = false;
                }

                parameter = parameter + " AND master.recSubSubSectorID IN (" + base.SubSubSectorIDs + ")";
                recSubSubSectorRepo subSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recSubSubSector> subSubSectors = subSubSectorDal.GetListMultipleSubSectorID(parameters);
                var result = subSubSectors;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else
            {
                int id = Convert.ToInt32(context.Request.Params["subsector_id"]);

                string form = context.Request.Params["aksikorporasi"];

                recDebtorSectorRepo debtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                List<recSubSubSector> subsubSectors = null;
                if (base.DebtorID.HasValue)
                {
                    subsubSectors = new List<recSubSubSector>();
                    var sectors = debtorSectorDal.GetListByDebtor(base.DebtorID.Value);
                    sectors.ForEach(delegate(recDebtorSector domain)
                    {
                        if (!subsubSectors.Exists(x => x.SubSubSectorID == domain.SubSubSectorID))
                        {
                            recSubSubSector subSubObj = new recSubSubSector();
                            subSubObj.SubSectorID = domain.SubSectorID;
                            subSubObj.SubSubSectorID = domain.SubSubSectorID;
                            subSubObj.SubSubSectorCode = domain.recSubSubSector.SubSubSectorCode;
                            subSubObj.SubSubSectorName = domain.recSubSubSector.SubSubSectorName;
                            subsubSectors.Add(subSubObj);
                        }
                    });
                }
                else
                {
                    subsubSectors = recSubSubSectorDal.GetList();
                }

                var result = subsubSectors.Where(x => x.SubSectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}